// pages/menus/menus.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deskId: '',
    deskCaption: '',
    menuClassList:[],
    menuClassId:1001,
    menuList:[],
    // 选择的数字
    js:'',
    //数量
    menuCount:'',
    //符号
    fh:'',
    //lineno
    lineNo:'',
    //menuId
    menuId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 获取传过来的参数
    this.setData({
      deskId: options.deskId,
      deskCaption: options.deskCaption,
    })
    this.getMenuClass(this.data.deskId)
    this.getMenuList(1001)
  },
  //获取楼层
  getMenuClass(id){
    wx.request({
      url: 'http://www.szyzit.com:8080/CateringSystem1.0/recruitment/catering/qryMenuClassList.do?method=login',
      header: {
        'Content-type': 'application/x-www-form-urlencoded'
      },
      method:'post',
      data:{
        deskId: id,
      },
      success:(res)=>{
       
        this.setData({
          menuClassList:res.data.responseBody.menuClassList
        })
      },
      fail:(res)=>{
        wx.showToast({
          title: '请求失败',
        })
      }
    })
  },
  //返回上一页
  toback() {
    wx.navigateBack({
      delta: 1
    })
  },
  //获取菜品
  getMenuList(id){
    wx.request({
      url: 'http://www.szyzit.com:8080/CateringSystem1.0/recruitment/catering/qryMenuListByDesk.do?method=login',
      method:'POST',
      header:{
        'Content-type': 'application/x-www-form-urlencoded'
      },
      data:{
        menuClassId:id,
        deskId:this.data.deskId,
        pageIndex:1,
        pageSize:100
      },
      success:(res)=>{
        this.setData({
          menuList:res.data.responseBody.menuList
        })
      },
      fail:(res)=>{
        wx.showToast({
          title: '请求失败',
        })
      }
    })
  },
//点击菜谱
classClick(e){
  console.log(e.currentTarget.dataset.menuclassid)
  this.setData({
    menuClassId:e.currentTarget.dataset.menuclassid
  })
  this.getMenuList(e.currentTarget.dataset.menuclassid)
},
onSelect(e){
  // console.log(e.currentTarget.dataset.menuid)
  // console.log(e.currentTarget.dataset.menustatus)
  // console.log(e.currentTarget.dataset.menuname)
  // console.log(e.currentTarget.dataset.selectedflag)
  if(e.currentTarget.dataset.selectedflag=='0'){
    //勾选
    wx.request({
      url: 'http://www.szyzit.com:8080/CateringSystem1.0/recruitment/catering/orderMenu.do?method=login',
      method:"POST",
      header:{
        'Content-type': 'application/x-www-form-urlencoded'
      },
      data:{
        menuId:e.currentTarget.dataset.menuid,
        deskId:this.data.deskId,
        deskCaption:e.currentTarget.dataset.menuname
      },
      success:(res)=>{
        console.log('勾选')
        this.getMenuList(this.data.menuClassId)
        this.getMenuClass(this.data.deskId)
      },
      fail:(res)=>{
        wx.showToast({
          title: '请求失败',
        })
      }
    })
  }else{
    //取消勾选
    if(e.currentTarget.dataset.menustatus != 0){
      wx.showToast({
        title:'已下单，不能取消' ,
      })
      return
  }
    wx.request({
      url: 'http://www.szyzit.com:8080/CateringSystem1.0/recruitment/catering/cancleOrderMenu.do?method=login',
      method:"POST",
      header:{
        'Content-type': 'application/x-www-form-urlencoded'
      },
      data:{
        lineNo:'',
        menuId:e.currentTarget.dataset.menuid,
        deskId:this.data.deskId,
      },
      success:(res)=>{
        console.log('取消勾选')
        this.getMenuList(this.data.menuClassId)
        this.getMenuClass(this.data.deskId)
      },
      fail:(res)=>{
        wx.showToast({
          title: '请求失败',
        })
      }
    })
  }
},
//酒水
checkCount(e){
  this.setData({
    fh:e.currentTarget.dataset.fh,
    menuCount:e.currentTarget.dataset.menucount||0,
    lineNo:e.currentTarget.dataset.lineno,
    menuId:e.currentTarget.dataset.menuid,
  })
},
//子组件传过来的值
thismyevent(e){
  console.log('选择的数字',e.detail)
  console.log('linenol',e.currentTarget)
  this.setData({
    js:e.detail
  })
  //判断加减
  if(this.data.fh=='-'){
    //减
    this.setData({
      menuCount:this.data.menuCount*1-e.detail*1
    })
    wx.request({
      url: 'http://www.szyzit.com:8080/CateringSystem1.0/recruitment/catering/subAccumlateMenu.do?method=login',
      method:'post',
      header:{'Content-type': 'application/x-www-form-urlencoded'},
      data:{
        lineNo:this.data.lineNo,
        menuCount:this.data.menuCount,
        accumlateFlag:1
      },
      success:(res)=>{
        this.setData({
          menuCount:''
        })
        this.getMenuList(6001)
        this.getMenuClass(this.data.deskId)
      },
      fail:(res)=>{
        wx.showToast({
          title: '请求失败',
        })
      }
    })
  }else{
    this.setData({
      menuCount:this.data.menuCount*1+e.detail*1
    })
    wx.request({
      url: 'http://www.szyzit.com:8080/CateringSystem1.0/recruitment/catering/accumlateMenu.do?method=login',
      method:'post',
      header:{'Content-type': 'application/x-www-form-urlencoded'},
      data:{
        menuId:this.data.menuId,
        deskId:this.data.deskId,
        deskCaption:this.data.deskCaption,
        lineNo:this.data.lineNo,
        menuCount:this.data.menuCount,
      },
      success:(res)=>{
        this.setData({
          menuCount:''
        })
        this.getMenuList(6001)
        this.getMenuClass(this.data.deskId)
      },
      fail:(res)=>{
        wx.showToast({
          title: '请求失败',
        })
      }
    })
  }
},
//下单
xiadan(){
  wx.navigateTo({
    url: '/pages/order/order?deskId='+this.data.deskId+'&deskCaption='+this.data.deskCaption,
  })
},
onShow(){
  // let pages = getCurrentPages();
  // let currPage = pages[pages.length - 1];
}
})